namespace C4F.DevKit.Bluetooth
{
    namespace ServiceAndDeviceDiscovery
    {
        #region Using Directives
        using System;
        using System.Collections.Generic;
        using System.Text;
        using System.Net.Sockets;
        using Resources;
        #endregion


        /// <summary>
        /// Returns the network stream that allows users to control 
        /// communication with a local service.
        /// </summary>
        public class ServiceHandle
        {
            #region Private Members

            /// <summary>
            /// Represents the socket used for communicating with clients.
            /// </summary>
            Socket serverSocket;

            #endregion

            #region Constructor and Destructor

            /// <summary>
            ///Initializes a ServiceHandle object.
            /// </summary>
            /// <param name="socket">socket for communicating with client.</param>
            public ServiceHandle(Socket socket)
            {
                this.serverSocket = socket;
            }

            /// <summary>
            ///Destructor for ServiceHandle which closes the socket connection.
            /// </summary>
            ~ServiceHandle()
            {
                this.serverSocket.Close();
            }

            #endregion

            #region Public Functions

            /// <summary>
            /// Determines if there are any connections pending on the socket.
            /// </summary>
            /// <returns>Returns true if connections are pending else returns 
            /// false.</returns>
            public bool ConnectionsPending()
            {
                return this.serverSocket.Poll(0, SelectMode.SelectRead);
            }

            /// <summary>
            ///Socket accepts connection and returns a network stream.
            /// </summary>
            /// <returns>A network stream which is used for communication 
            /// with clients.</returns>            
            /// <exception cref="ServiceConnectionException">Thrown  
            /// when the local service is unable to accept the connection. 
            /// </exception> 
            public NetworkStream AcceptConnection()
            {
                Socket socket = null;
                NetworkStream netwrkStream = null;
                try
                {
                    socket = this.serverSocket.Accept();
                    netwrkStream =
                        new NetworkStream(socket, true);
                }
                catch (Exception ex)
                {
                    ServiceConnectionException servEx =
                        new ServiceConnectionException(
                            SDPMessages.ErrorMsgAccConn +
                            ex.Message);
                    throw servEx;
                }
                return netwrkStream;
            }

            #endregion
        }

    }

}
